home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 854 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  88 lines

  1. Path: engnews1.Eng.Sun.COM!usenet
  2. From: jgealow@mtl.mit.edu (Jeffrey C. Gealow)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: initialization of nonlocal static objects
  5. Date: 26 Mar 1996 00:30:05 GMT
  6. Organization: MIT Microsystems Technology Laboratories
  7. Sender: jgealow@gator.mit.edu
  8. Approved: clamage@eng.sun.com (comp.std.c++)
  9. Message-ID: <ky3f6w679u.fsf@gator.mit.edu>
  10. References: <4j1tm6$bmu@senator-bedfellow.MIT.EDU>
  11.     <9603251201.AA24987@lts.sel.alcatel.de>
  12. NNTP-Posting-Host: taumet.eng.sun.com
  13. X-Nntp-Posting-Host: gator.mit.edu
  14. In-Reply-To: James Kanze US/ESC 60/3/141 #40763's message of 25 Mar 1996
  15.     16:16:43 GMT
  16. X-Newsreader: Gnus v5.1
  17. Content-Length: 1587
  18. X-Lines: 61
  19.  
  20. In article <9603251201.AA24987@lts.sel.alcatel.de> James Kanze 
  21. US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> writes:
  22.  
  23.   In article <4j1tm6$bmu@senator-bedfellow.MIT.EDU> jgealow@mtl.mit.edu
  24.   (Jeffrey C. Gealow) writes:
  25.  
  26.   |>   The technique described in the ARM 3.4 annotation cannot be applied to
  27.   |>   const objects since the values of the objects may not be changed.
  28.  
  29.   No.  Const has nothing to do with the question, since objects only
  30.   become const after construction.
  31.  
  32. I don't understand James Kanze's comment.  Consider:
  33.  
  34.   // file nifty_library.h:
  35.   //
  36.  
  37.   class X {
  38.   public:
  39.     int i;
  40.     X() : i(10) {};
  41.   };
  42.  
  43.   extern const X obj;
  44.  
  45.   class nifty_counter {
  46.     static nifty_count;
  47.   public:
  48.     nifty_counter() {
  49.       if (nifty_count++ == 0) {
  50.         obj.i = 5;
  51.       }
  52.     }
  53.   };
  54.  
  55.  
  56.   // file nifty_library.cc:
  57.   //
  58.  
  59.   #include "nifty_library.h"
  60.  
  61.   const X obj;
  62.  
  63. The intent of the statement obj.i = 5 in nifty_counter::nifty_counter() 
  64. is to "initialize" obj.  But formally, obj is initialized by the 
  65. default constructor for class X.
  66.  
  67. % CC -c nifty_library.cc
  68. "nifty_library.h", line 17: Error: The left operand cannot be assigned to.
  69. 1 Error(s) detected.
  70. % CC -Dconst="" -c nifty_library.cc
  71.  
  72. Perhaps the problem may be circumvented by casting away const: ((X) obj).i = 5;
  73.  
  74.    I believe that the intent of the current draft is that rule in ARM 3.4
  75.    hold only for uses invoked directly or indirectly from main, but not
  76.    for uses which result from the construction of other static objects.
  77.  
  78. Perhaps the intent should be clarified in the text of the draft.
  79.  
  80. Jeff
  81.  
  82.  
  83. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  84. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  85. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  86. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  87. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  88.